package com.cweb.admin.configuration.database;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DataSourceUtil {
    /**
     * JDBC连接数据库，生成数据对象
     * 配置对象
     */
    static public DataSource initDataSource(DataSourceDTO dataSourceParam) {
        // 参数判断
        if (StrUtil.isBlank(dataSourceParam.getUsername()) || StrUtil.isBlank(dataSourceParam.getPassword()) ||
                StrUtil.isBlank(dataSourceParam.getAddr()) || StrUtil.isBlank(dataSourceParam.getDbName())) {
            return null;
        }
        // 建立dataSource对象
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://" + dataSourceParam.getAddr() + ":" + dataSourceParam.getPort() + "/" + dataSourceParam.getDbName() + "?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Hongkong");
        dataSource.setUsername(dataSourceParam.getUsername());
        dataSource.setPassword(dataSourceParam.getPassword());
        // 连接测试
        if (!DataSourceUtil.testConnection(dataSource)) return null;
        return dataSource;
    }

    /**
     * JDBC连接数据库，生成数据对象
     * 通过配置文件
     */
    static public DataSource initDataSource(DataSourceProperties dataSourceProp) {
        // 参数判断
        if (StrUtil.isBlank(dataSourceProp.getDriverClassName()) || StrUtil.isBlank(dataSourceProp.getUrl()) ||
                StrUtil.isBlank(dataSourceProp.getUsername()) || StrUtil.isBlank(dataSourceProp.getPassword())) {
            return null;
        }
        // 建立dataSource对象
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(dataSourceProp.getDriverClassName());
        dataSource.setUrl(dataSourceProp.getUrl());
        dataSource.setUsername(dataSourceProp.getUsername());
        dataSource.setPassword(dataSourceProp.getPassword());
        // 连接测试
        if (!DataSourceUtil.testConnection(dataSource)) return null;
        return dataSource;
    }

    /**
     * 数据库连接测试
     */
    static public boolean testConnection(DataSource dataSource) {
        // 连接测试
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        try {
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery("select 'a' from dual");
            return true;
        } catch (SQLException e) {
            return false;
        } finally {
            try {
                if (rset != null) rset.close();
            } catch (Exception e) {
            }
            try {
                if (stmt != null) stmt.close();
            } catch (Exception e) {
            }
            try {
                if (conn != null) conn.close();
            } catch (Exception e) {
            }
        }
    }
}
